﻿<!-- comments  -->

{% if entry.allow_comment %}
<script type="text/javascript" src="/static/xheditor/xheditor.js"></script>
<script type="text/javascript">
function xheditorInit(on)
{
    if(on){
        $('#comment').xheditor(true,{
            tools:'GStart,Blocktag,Bold,Italic,Underline,Removeformat,GEnd,Separator,GStart,List,Link,Unlink,Img,Emot,Table,GEnd,Separator,GStart,Source,Fullscreen,About,GEnd',
            forcePtag:false
        });
    }else{
        $('#comment').xheditor(false);
    }
}
</script>
<a name="comments"></a> <!--回复使用的锚-->
<div id="comments-wrap">

    <h2 id="respond">{{ entry.commentcount }} 条评论</h2><span class="goto-comment"><a href="#commentform">我要留言</a></span>
        {% if entry.comments %}
        <ul id="thecomments">
        {% for comment in entry.comments %}
        <li id="comment-{{comment.key.id}}" class="comment">
            <div class="commentleft">
                <div class="commentcount"> {{ forloop.counter }} F </div>
                <div id="gravatar"><img alt='' src='{{comment.gravatar_url}}' class='avatar avatar-36 photo avatar-default' height='36' width='36' /></div>
            </div>
            <div class="commentright">
                <p class="header" > 
                    {%if comment.weburl %}
                    <a id="comment-{{comment.key.id}}" class="url" target="_blank" href="{{comment.weburl}}">{{comment.author}}</a>
                    {% else %}{{comment.author}}{%endif%} 发表于 
                    {{comment.date|datetz:"Y-m-d"}} at {{comment.date|datetz:"H:i"}}
                    {% if comment.reply_notify_mail %}
                    <a onclick="return backcomment('{{comment.author}}','{{comment.key.id}}')" href="#commentform" title="@{{comment.author}}-{{comment.key.id}}:" style="color:red">回复</a> 
                    {% else %}
                    <a onclick="return backcomment('{{comment.author}}','{{comment.key.id}}')" href="#commentform" title="@{{comment.author}}-{{comment.key.id}}:">回复</a> 
                    {% endif %}
                    <a onclick="return quote('{{comment.author}}','commenttext-{{comment.key.id}}')" href="#commentform">引用</a>
                </p>
                <div class="commenttext" id="commenttext-{{comment.key.id}}">{{comment.content}}</div>
            </div>  
        </li>
        {% endfor %}
        </ul>
        {% endif %}

    <form action="/post_comment" method="post" id="commentform">
        <div id="conmmentform">
            <div id="conmmentforminfo">
                <label class="conmmentform-title" for="author">昵称(必填)</label> 
                <div><input type="text" name="author" id="author" value="" tabindex="1" /></div>
                <label class="conmmentform-title" for="email">Email(必填，绝不公开)</label>
                <div><input type="text" name="email" id="email" value="" tabindex="2" /></div> 
                <label class="conmmentform-title" for="url">网站</label>
                <div> <input type="text" name="url" id="url" value="" tabindex="3" /></div>
                
                <div id="checkarea">
                <label class="conmmentform-title" for="checkret"><span id="disp_check">麻烦计算：{{checknum1}}+{{checknum2}}=?</span></label>
                <input type="text" name="checkret" id="checkret" value="" tabindex="4" />
                <input type="hidden" name="checknum" id="checknum" value="{{checknum1}}+{{checknum2}}" />
                </div>
            </div>
            <div id="conmmentformtext">
                <label for="comment"  class="conmmentform-title">留言区（打开编辑器？<input type="checkbox" onclick="$(xheditorInit(this.checked))" />）</label> 
                <div><textarea name="comment" id="comment" rows="10" cols="10" tabindex="5"></textarea></div>
                <p>

                    <input type="hidden" name="key" value="{{entry.key}}" />
                    <input type="hidden" name="useajax" value="1" />
                    <input type="submit"  name="submit" id="submit" value="" class="comment-button" tabindex="6" />
                    <input name="reply_notify_mail" id="reply_notify_mail" tabindex="7" 
                        style="width: auto;" type="checkbox" />
                    <label for="reply_notify_mail">有回复时邮件通知我</label>

                </p>
            </div>
            <div id="s_msg" style="display:none;color:red">正在提交留言...</div>
        </div>
<script type="text/javascript" src="/static/js/jquery.cookie.js"></script>
<script type="text/javascript" src="/static/js/jquery.form.utf8.js"></script>
<script type="text/javascript">
<!--//
    loadjs=false;

    commentuser=$.cookie('comment_user');
    if (commentuser)
    {
        //[user,email,url]=commentuser.split('#@#');
        var tuple=commentuser.split('#@#');
        var user,email,url;
        user=tuple[0];
        email=tuple[1];
        url=tuple[2];
        $('#author').val(user);
        $('#email').val(email);
        $('#url').val(url);

        $('#checkarea').hide();
        
    }
    else
    {
        num1=Math.round(Math.random()*10);
        num2=Math.round(Math.random()*10);
        $('#checkarea #checknum').val(num1+'+'+num2);
        $('#checkarea #disp_check').text('麻烦计算：'+num1+'+'+num2+'=?');
        $('#checkarea').show();
    }

    function showinfo(msg)
    {
        alert(msg);
    }
    //回复自动输入姓名函数
    function backcomment(name,id){
        backdb=document.getElementById('comment');
        //backdb.value+="<a name='@"+name+'-'+id+":' href='#comment-"+id+"'>"+'@'+name+':'+'</a>\n';
        backdb.value+="@"+name+"-"+id+":\n";
        return true;
    }
    //引用调用函数
    function quote(name,id){
        var quoteMsg=document.getElementById(id).innerHTML;
        document.getElementById("comment").value+='引用'+name+'<blockquote>'+quoteMsg+'</blockquote>\n';
        return true;
    }
    function add_comment(msg)
    {
        comment=$(msg)
        if (!loadjs)
        {
            $("#thecomments").append(comment).show();
            $.getScript("/static/js/jquery.color.js", function(){
                comment.animate( { backgroundColor: '#fbc7c7' }, "slow")
                .animate( { backgroundColor: 'white' }, "slow")
                loadjs=true;
            });
        }else
        {
            $("#thecomments").append(comment);
            comment.animate( { backgroundColor: '#fbc7c7' }, "slow")
            .animate( { backgroundColor: 'white' }, "slow")

        }
    }

    $('#commentform').ajaxForm({
        type:'post',
        dataType:  'json',
        beforeSubmit:function(formData,jqForm,options){
            var form = jqForm[0];
            if (form.author)
            {

                if (!form.author.value)
                {
                    showinfo('请输入你的名字!');
                    form.author.focus();
                    return false;
                }
                if (!form.email.value)
                {
                    showinfo('请输入邮件地址');
                    form.email.focus();
                    return false;
                }
            }
            if ($('#checkarea').css('display')!='none')
            {
                if(eval(form.checknum.value)!=form.checkret.value)
                {
                    alert('计算错误，这个算术好像在幼儿园学过的吧？');
                    form.checkret.value='';
                    form.checkret.focus();
                    return false;
                }
            }
            
            if (!form.comment.value)
            {
                showinfo('请输入留言内容');
                form.comment.focus();
                return false;
            }

            $('#s_msg').text('正在提交留言...').show();

            return true;

        },
        success:function(data){
            if (data[0])
            {
                /*document.cookie=data[2];*/
                add_comment(data[1]);
                $('#s_msg').text('留言提交成功！');
                $('#comment').val('');
            }
            else if (data[1]==-102)
            {
                showinfo('计算错误，回幼儿园进修一下再来过。');
                $('#s_msg').text('留言提交失败！');
                $('#checkret').focus();
            }
            else if (data[1]==-101)
            {
                showinfo(data[2]);
                $('#s_msg').text('留言提交失败！');
            }
        }
    });
//-->
</script>
    </form>
</div> 
{% else %}
<div id="comments-wrap"><p class="nocomments">评论已关闭</p></div>
{% endif %}

<!-- /comments -->
